package com.ulic.customerManApplication.journeylovemodule.dao.impl;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Component;

import com.ulic.customerManApplication.base.BaseDao;
import com.ulic.customerManApplication.constant.CONSTANT;
import com.ulic.customerManApplication.journeylovemodule.bo.JourneyOfLoveActivity;
import com.ulic.customerManApplication.journeylovemodule.dao.JourneyLoveDao;
import com.ulic.customerManApplication.journeylovemodule.vo.JourneyLoveVO;
import com.ulic.customerManApplication.pager.PagerModel;
import com.ulic.customerManApplication.usermodule.bo.User;

@Component("journeyLoveDao")
public class JourneyLoveDaoImpl extends BaseDao implements JourneyLoveDao {
	Session session = null;

	@Override
	public void insertJourneyLove(JourneyOfLoveActivity journeyOfLoveActivity)
			throws Exception {
		// TODO Auto-generated method stub

		session = getHibernateTemplate().getSessionFactory().getCurrentSession();
		try{
			session.save(journeyOfLoveActivity);
		}catch (Exception e) {
			e.printStackTrace();
		}
		session.flush();
	}

	@Override
	public void insertJourneyLoves(
			List<JourneyOfLoveActivity> journeyOfLoveActivities)
			throws Exception {
		// TODO Auto-generated method stub

		session = getHibernateTemplate().getSessionFactory().getCurrentSession();
		for(JourneyOfLoveActivity journeyOfLoveActivity:journeyOfLoveActivities){
			session.save(journeyOfLoveActivity);
		}
		session.flush();
	}

	@Override
	public JourneyOfLoveActivity getJourneyLoveById(int id) {
		// TODO Auto-generated method stub
		session = getHibernateTemplate().getSessionFactory().getCurrentSession();
		Criteria cri = session.createCriteria(JourneyOfLoveActivity.class);
		List list = cri.add(Restrictions.eq("id", id)).list();
		JourneyOfLoveActivity journeyOfLoveActivity = null;
		if(list.size()>0){
			journeyOfLoveActivity	= (JourneyOfLoveActivity)list.get(0);
		}
		return journeyOfLoveActivity;
	}

	@Override
	public PagerModel getJourneyLoves(int curPage) {
		// TODO Auto-generated method stub
		
		String hql = "from JourneyOfLoveActivity jla order by createDate desc";
		return searchPaginated(hql, curPage, CONSTANT.PageSize);
	}

	@Override
	public void updateJourneyLove(JourneyOfLoveActivity journeyOfLoveActivity)
			throws Exception {
		// TODO Auto-generated method stub

		session = getHibernateTemplate().getSessionFactory().getCurrentSession();
		session.saveOrUpdate(journeyOfLoveActivity);
		session.flush();
	}

	@Override
	public void deleteJourneyLove(JourneyOfLoveActivity journeyOfLoveActivity) throws Exception {
		// TODO Auto-generated method stub

		session = getHibernateTemplate().getSessionFactory().getCurrentSession();
		session.delete(journeyOfLoveActivity);
		session.flush();
	}

	@Override
	public JourneyOfLoveActivity getJourneyOfLoveActivityByDate(User user,
			String activityDate) {
		// TODO Auto-generated method stub
		session = getHibernateTemplate().getSessionFactory().getCurrentSession();
		Criteria cri = session.createCriteria(JourneyOfLoveActivity.class);
		List list = cri.createAlias("user", "user").add(Restrictions.eq("user.um_user_id", user.getUm_user_id())).add(Restrictions.eq("activityDate", activityDate.trim())).list();
		JourneyOfLoveActivity journeyOfLoveActivity = null;
		if(list.size() >0){
			journeyOfLoveActivity = (JourneyOfLoveActivity)list.get(0);
		}
		return journeyOfLoveActivity;
	}

	@Override
	public PagerModel getJourneyLoves(
			JourneyLoveVO journeyLoveVO, int curPage) {
		// TODO Auto-generated method stub
		
		StringBuilder sb = new StringBuilder();
		
		sb.append(" from JourneyOfLoveActivity jActivity where 1=1");
		if(journeyLoveVO != null){
			if(journeyLoveVO.getStartDate() != null && journeyLoveVO.getStartDate().length() != 0){
				sb.append(" and to_date(jActivity.activityDate,'YYYY-MM-DD  HH24:MI:SS') >= to_date('"+journeyLoveVO.getStartDate()+" 00:00:00"+"','YYYY-MM-DD  HH24:MI:SS') ");
			}
			if(journeyLoveVO.getEndDate() != null && journeyLoveVO.getEndDate().length() != 0){
				sb.append(" and to_date(jActivity.activityDate,'YYYY-MM-DD  HH24:MI:SS') <= to_date('"+journeyLoveVO.getEndDate()+" 23:59:59"+"','YYYY-MM-DD  HH24:MI:SS') ");
			}
			if(journeyLoveVO.getJourneyOfLoveActivity()!=null){
				JourneyOfLoveActivity jActivity = journeyLoveVO.getJourneyOfLoveActivity();
				if(jActivity.getSubCompany()!=null&&!"No".equals(jActivity.getSubCompany())){
					sb.append(" and jActivity.subCompany = "+jActivity.getSubCompany());
				}
				if(jActivity.getSubCity()!=null&&!"No".equals(jActivity.getSubCity())){
					sb.append(" and jActivity.subCity = "+jActivity.getSubCity());
				}
				if(jActivity.getOaSph() != null && !"No".equals(jActivity.getOaSph().trim()) && !"".equals(jActivity.getOaSph().trim())){
					sb.append(" and jActivity.oaSph like '%"+jActivity.getOaSph().trim() + "%'");
				}
			}
		}
		sb.append(" order by activityDate desc,createDate desc");
		return searchPaginated(sb.toString(), curPage, 10);
	}

	@Override
	public PagerModel getJourneyLoves(JourneyLoveVO journeyLoveVO, int curPage,
			int pageSize) {
        StringBuilder sb = new StringBuilder();
		
		sb.append(" from JourneyOfLoveActivity jActivity where 1=1");
		if(journeyLoveVO != null){
			if(journeyLoveVO.getStartDate() != null && journeyLoveVO.getStartDate().length() != 0){
				sb.append(" and to_date(jActivity.activityDate,'YYYY-MM-DD  HH24:MI:SS') >= to_date('"+journeyLoveVO.getStartDate()+" 00:00:00"+"','YYYY-MM-DD  HH24:MI:SS') ");
			}
			if(journeyLoveVO.getEndDate() != null && journeyLoveVO.getEndDate().length() != 0){
				sb.append(" and to_date(jActivity.activityDate,'YYYY-MM-DD  HH24:MI:SS') <= to_date('"+journeyLoveVO.getEndDate()+" 23:59:59"+"','YYYY-MM-DD  HH24:MI:SS') ");
			}
			if(journeyLoveVO.getJourneyOfLoveActivity()!=null){
				JourneyOfLoveActivity jActivity = journeyLoveVO.getJourneyOfLoveActivity();
				if(jActivity.getSubCompany()!=null&&!"No".equals(jActivity.getSubCompany())){
					sb.append(" and jActivity.subCompany = "+jActivity.getSubCompany());
				}
				if(jActivity.getSubCity()!=null&&!"No".equals(jActivity.getSubCity())){
					sb.append(" and jActivity.subCity = "+jActivity.getSubCity());
				}
				if(jActivity.getOaSph() != null && !"No".equals(jActivity.getOaSph().trim()) && !"".equals(jActivity.getOaSph().trim())){
					sb.append(" and jActivity.oaSph like '%"+jActivity.getOaSph().trim() + "%'");
				}
			}
		}
		sb.append(" order by activityDate desc,createDate desc");
		return searchPaginated(sb.toString(),curPage,pageSize);
	}

}
